package com.ruoyi.web.controller.smy;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.smy.SecurityWebUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.dto.OptDto;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.smy.ISecurityWebUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 微信小程序会员
 * @classname MemberController
 * @author ZhanCheng.Li
 * @date 2022/10/20 15:36
 * @version 1.0.0
 */
@RestController
@RequestMapping("/smy/member")
public class MemberController extends BaseController {
	@Resource
	private ISecurityWebUserService securityWebUserService;

	@PreAuthorize("@ss.hasPermi('system:user:list')")
	@GetMapping("/list")
	public TableDataInfo list(SecurityWebUser securityWebUser) {
		startPage();
		//查询所有微信小程序会员信息
		securityWebUser.setRoleId(3L);
		List<SecurityWebUser> list = securityWebUserService.selectMicroUserPage(securityWebUser);
		return getDataTable(list);
	}


	@PreAuthorize("@ss.hasPermi('system:user:remove')")
	@Log(title = "升级为加盟商", businessType = BusinessType.DELETE)
	@PostMapping("/upgrade")
	public AjaxResult upgrade(@RequestBody OptDto optDto) {
		try {
			if(StrUtil.isNotEmpty(optDto.getOptId()) && StrUtil.isNotEmpty(optDto.getOptName())){
				securityWebUserService.upgradeDistributor(optDto.getOptId(),optDto.getOptName());
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return AjaxResult.error(e.getMessage());
		}
		return AjaxResult.success();
	}

	@PreAuthorize("@ss.hasPermi('smy:user:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(securityWebUserService.selectMicroUserById(id));
	}


}
